/*
 * Copyright (C) 2013 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.ui.components;

import rpg.gfx.Bitmap;

/**
 *
 * @author rothens
 */
public class ComponentScrollpanel extends ComponentPanel implements IClickableComponent, IDraggableComponent {

    public ComponentScrollBar vertical;
    public ComponentScrollBar horizontal;
    public Bitmap contentArea;
    

    public ComponentScrollpanel(int width, int height, Integer x, Integer y) {
        this(width, height, x, y, false, false);
    }

    public ComponentScrollpanel(int width, int height, Integer x, Integer y, boolean rightScroll, boolean bottomScroll) {
        super(width, height, x, y);
        
        
        if(bottomScroll){
            horizontal = new ComponentScrollBar(5, height-5-StaticTextures.hscroll[0].height,  width-10-((rightScroll)?StaticTextures.vscroll[0].width:0), true);
            horizontal.setParent(this);
        }
        
        if(rightScroll){
            vertical = new ComponentScrollBar( width-5-StaticTextures.vscroll[0].width, 5,  height-10-((bottomScroll)?StaticTextures.hscroll[0].height:0), false);
            vertical.setParent(this);
        }
        contentArea = new Bitmap(width-10-((rightScroll)?StaticTextures.vscroll[0].width:0), height-10-((bottomScroll)?StaticTextures.hscroll[0].height:0));
        if(bottomScroll) {
            horizontal.redraw();
        }
        if(rightScroll){
            vertical.redraw();
        }
        
    }

    @Override
    public int getXDiff() {
        return (horizontal != null) ? horizontal.getLocation() - 5 : -5;
    }

    @Override
    public int getYDiff() {
        return (vertical != null) ? vertical.getLocation() - 5 : -5;
    }

    @Override
    public void addChild(Component child) {
        super.addChild(child);
        child.setParent(this);
        child.redraw();
        regenerateScrollBars();
        redraw();
    }

    @Override
    public void redraw() {
        clear();
        contentArea.clear();
        switch(state){
            case HOVER:
                draw(hover, 0, 0);                
                break;
            case INACTIVE:
                draw(inactive, 0, 0);
                break;
            case SELECTED:
                draw(active, 0, 0);
                break;
                    
        }
        if(vertical != null){
            //vertical.redraw();
            drawOn(vertical, vertical.x, vertical.y);
        }
        if(horizontal != null){
            //horizontal.redraw();
            drawOn(horizontal, horizontal.x, horizontal.y);
        }
        int xDiff = getXDiff() + 5;
        int yDiff = getYDiff() + 5;
        for(Component c : subcomponents){
            contentArea.drawOn(c, c.x-xDiff, c.y-yDiff);
        }
        drawOnBlend(contentArea, 5, 5);
        if(parent != null){
            ((Component)parent).redraw();
        }
    }

    @Override
    public boolean onPress(int x, int y) {
        return (horizontal != null && horizontal.onPress(x - this.x, y - this.y))
                || (vertical != null && vertical.onPress(x - this.x, y - this.y));
    }

    @Override
    public boolean onRelease(int x, int y) {
        int mx = x - this.x;
        int my = y - this.y;
        if(horizontal != null){
            horizontal.onRelease(mx, my);
        }
        if(vertical != null){
            vertical.onRelease(mx, my);
        }
        return false;
    }

    @Override
    public void onStartDrag(int x, int y) {
        int mx = x - this.x;
        int my = y - this.y;
        if(horizontal != null && horizontal.isMouseOver(mx, my)){
            horizontal.onStartDrag(mx, my);
        }
        if(vertical != null && vertical.isMouseOver(mx, my)){
            vertical.onStartDrag(mx, my);
        }
    }

    @Override
    public void whileDrag(int x, int y) {
        int mx = x - this.x;
        int my = y - this.y;
        if(vertical != null && vertical.isDragged()){
            vertical.whileDrag(mx, my);
        }
        
        if(horizontal != null && horizontal.isDragged()){
            horizontal.whileDrag(mx, my);
        }
    }

    @Override
    public void onEndDrag(int x, int y) {
        if(vertical != null && vertical.isDragged()){
            vertical.onEndDrag(x, y);
        }
        
        if(horizontal != null && horizontal.isDragged()){
            horizontal.onEndDrag(x, y);
        }
    }

    @Override
    public boolean isDragged() {
        return (vertical != null && vertical.isDragged()) || (horizontal != null && horizontal.isDragged());
    }

    public int getHorizontalWidth() {
        int ret = 0;
        for (Component c : subcomponents) {
            int right = c.x + c.width;
            if (right > ret) {
                ret = right;
            }
        }
        return ret;
    }

    public int getVerticalHeight() {
        int ret = 0;
        for (Component c : subcomponents) {
            int bottom = c.y + c.height;
            if (bottom > ret) {
                ret = bottom;
            }
        }
        return ret;
    }

    public void regenerateScrollBars() {
        if (vertical != null) {
            vertical.createKnob( contentArea.height, getVerticalHeight());
        }

        if (horizontal != null) {
            horizontal.createKnob( contentArea.width, getHorizontalWidth());
        }

    }
    
    public void redrawComponents() {
        for(Component c : subcomponents){
            c.redraw();
        }
    }
}
